/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * 说这个是深度，那我是前序深度
 * @param {TreeNode} root
 * @return {TreeNode}
 */
var invertTree = function(root) {
    invertLevel(root)
    return root
};

function invertLevel(root){
    if(root === null) return
    let temp = root.left 
    root.left = root.right
    root.right = temp
    invertLevel(root.left)
    invertLevel(root.right)
}


/**
 * 广度
 * @param {TreeNode} root
 * @return {TreeNode}
 */
var invertTree = function (root) {
    if (root === null) return root
    let queue = [root];

    while (queue.length > 0) {
        let len = queue.length,
            node;

        for (let i = 0; i < len; i++) {
            node = queue.shift()
            let temp = node.left
            node.left = node.right
            node.right = temp
            node.left && queue.push(node.left)
            node.right && queue.push(node.right)
        }
    }
    return root
};
